package extend.db.util;

import java.util.HashMap;
import java.util.Map;
import java.sql.Types;

/**
 * Created by xfw on 17-3-22.
 */
public class JavaTpye
{
    public static final String TYPE_INT = "java.lang.Integer";
    public static final String TYPE_LONG = "java.lang.Long";
    public static final String TYPE_BOOLEAN = "java.lang.Boolean";
    public static final String TYPE_CHAR = "java.lang.String";
    public static final String TYPE_FLOAT ="java.lang.Float";
    public static final String TYPE_DOUBLE = "java.lang.Double";
    public static final String TYPE_SHORT = "java.lang.Short";
    public static final String TYPE_BYTE = "java.lang.String";
    public static final String TYPE_STRING = "java.lang.String";

    private static Map<String, Integer> sqlTypes = new HashMap<String, Integer>() {{
        put(TYPE_INT, Types.INTEGER);
        put(TYPE_LONG, Types.BIGINT);
        put(TYPE_BOOLEAN, Types.BOOLEAN);
        put(TYPE_CHAR, Types.CHAR);
        put(TYPE_FLOAT, Types.FLOAT);
        put(TYPE_DOUBLE, Types.DOUBLE);
        put(TYPE_SHORT, Types.SMALLINT);
        put(TYPE_BYTE, Types.BIT);
        put(TYPE_STRING, Types.VARCHAR);
    }};

    public static int getSqlType(Object i)
    {
        String javaType = getType(i);
        if (sqlTypes.containsKey(javaType)) {
            return sqlTypes.get(javaType);
        } else {
            return Types.VARCHAR;
        }
    }

    public static String getType(Object o)
    {
        return o.getClass().getTypeName();
    }

    public static String getType(int i)
    {
        return TYPE_INT;
    }

    public static String getType(long i)
    {
        return TYPE_LONG;
    }

    public static String getType(boolean i)
    {
        return TYPE_BOOLEAN;
    }

    public static String getType(char i)
    {
        return TYPE_CHAR;
    }

    public static String getType(float i)
    {
        return TYPE_FLOAT;
    }

    public static String getType(double i)
    {
        return TYPE_DOUBLE;
    }

    public static String getType(short i)
    {
        return TYPE_SHORT;
    }

    public static String getType(byte i)
    {
        return TYPE_BYTE;
    }

    public static String getType(String i)
    {
        return TYPE_STRING;
    }





}
